package com.nc.delivery_project.server.dao;

import com.nc.delivery_project.server.domain.Currency;
import com.nc.delivery_project.server.domain.Role;
import com.sun.javafx.scene.control.skin.FXVK;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by miroshnik on 1/28/2015.
 */
public class CurrencyDao extends AbstractDao<Currency> {

    public static final String CURRENCY_ID = "currency_id";
    public static final String CURRENCY_TITLE = "currency_title";
    public static final String CURRENCY_RATE = "currency_rate";
    public static final String ENTITY_NAME = Currency.class.getSimpleName();

    public CurrencyDao() {
        super(ENTITY_NAME);
    }

    public CurrencyDao(Connection connection) throws SQLException {
        this(connection, ENTITY_NAME);
    }

    public CurrencyDao(Connection connection, String entityName) throws SQLException {
        super(connection, entityName);
    }

    @Override
    protected void prepareInsertStatement(CallableStatement statement, Currency object) throws SQLException {
        statement.setString(1, object.getTitle());
        statement.registerOutParameter(2, Types.NUMERIC);
        statement.setString(3, object.getTitle());
        statement.setString(4, String.valueOf(object.getRate()));
    }

    @Override
    protected void prepareUpdateStatement(CallableStatement statement, Currency object) throws SQLException {
        statement.setInt(1, object.getId());
        statement.setString(2, object.getTitle());
        statement.setString(3, object.getTitle());
        statement.setDouble(4, object.getRate());
    }

    @Override
    protected List<Currency> parseResultSet(ResultSet rs) throws SQLException {
        List<Currency> currencies = new ArrayList<>();
        while (rs.next()) {
            int currencyId = rs.getInt(CURRENCY_ID);
            String title = rs.getString(CURRENCY_TITLE);
            double rate = Double.parseDouble(rs.getString(CURRENCY_RATE));

            currencies.add(new Currency(currencyId, title, rate));
        }

        return currencies;
    }
}
